package com.hjy.elasticsearch.controller;

import com.hjy.elasticsearch.entity.ESEntity;
import com.hjy.elasticsearch.entity.Entity;
import com.hjy.elasticsearch.entity.OrderEntity;
import com.hjy.elasticsearch.service.CityESService;
import io.searchbox.core.Index;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class EsController {

    @Autowired
    private CityESService cityESService;

    @RequestMapping(value = "saveone",method = RequestMethod.POST)
    @ResponseBody
    public Object saveOne(Long i,String name,String surname){

        Entity e1 = new Entity(i,name,surname);
        Index index = cityESService.saveEntity(e1);

        return  index;
    }

    @RequestMapping(value = "savelist",method = RequestMethod.POST)
    @ResponseBody
    public Object saveList(int i){
        List<Entity> list = new ArrayList<Entity>();

        for(int j=0;j<=i;j++){
            Entity e1 = new Entity(Long.valueOf(j),"名："+j,"姓："+j);
            list.add(e1);
        }


        cityESService.saveEntity(list);

        return  null;
    }

    @RequestMapping(value = "find",method = RequestMethod.POST)
    @ResponseBody
    public List find(String index,String msg,String tableName){
        List list = cityESService.searchEntity(index,msg,tableName);
        return list;
    }

    /**
     * 两列查询
     * @param index
     * @param index2
     * @param msg
     * @return
     */
    @RequestMapping(value = "findtwo",method = RequestMethod.POST)
    @ResponseBody
    public List findTwo(String index,String index2,String msg){
        List<Entity> list = (List<Entity>)cityESService.searchTwo(index,index2,msg);
        return list;
    }

    /**
     * 多列查询
     * @param msg
     * @param index
     * @return
     */
    @RequestMapping(value = "findmore",method = RequestMethod.POST)
    @ResponseBody
    public List findmore(int pageNumber,int pageSize,String msg,String index,String index2){
        List<Entity> list = null;
        if(index!=null && !"".equals(index)){
            if(index2!=null && !"".equals(index2)){
                list = (List<Entity>)cityESService.searchmore(pageNumber,pageSize,msg,index,index2);
            }else{
                list = (List<Entity>)cityESService.searchmore(pageNumber,pageSize,msg,index);
            }
        }else{
            if(index2!=null && !"".equals(index2)){
                list = (List<Entity>)cityESService.searchmore(pageNumber,pageSize,msg,index2);
            }
        }
        return list;
    }

}


























